/*
 *     Copyright (c) 2021. cc01cc. All rights reserved.
 *
 *     This file is part of this file.
 *
 *     This file is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This file is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 0
#define F 1
#define n_round 3

#define Person_name char
// typedef char[23] Person_name
struct Circular_linked_list
{
    struct Circular_linked_list* next_node;
    Person_name name[23];
};
struct N_people
{
    int n_f;
    int n_m;
    int min_people;
    int max_people;
};
typedef struct Circular_linked_list* Linked_list;
typedef struct Circular_linked_list* Node;
typedef struct N_people N_people;

N_people getData(Linked_list f_list, Linked_list m_list);
printRound(Linked_list f_list, Linked_list m_list, N_people n_people);
int main()
{
    Linked_list f_list = (Linked_list)malloc(sizeof(struct Circular_linked_list));
    Linked_list m_list = (Linked_list)malloc(sizeof(struct Circular_linked_list));
    Node f_first = f_list;
    Node m_first = m_list;
    N_people n_people;
    // printf("The f_list is %p\n", f_list);
    // printf("The m_list is %p\n", m_list);
    n_people = getData(f_list, m_list);
    if(n_people.min_people == 0) return 0;
    // printf("min max people is\t%d\t%d\n", n_people.max_people, n_people.min_people);
    // printf("f_list->name is %s\n", f_list->name);
    // printf("The f_list is %p\n", f_list);
    // printf("m_list->name is %s\n", m_list->name);
    // printf("The m_list is %p\n", m_list);
    f_list = f_first;
    m_list = m_first;
    printRound(f_list, m_list, n_people);
    return 0;
}

void cutName(Person_name* name);
void initial(Linked_list list, Person_name* name);
Linked_list insert(Linked_list list, Person_name* name);
int determineGender(Person_name* name);
/*
*   @return the least number of genders.
*/
N_people getData(Linked_list f_list, Linked_list m_list)
{
    int n_data;
    int i;
    int n_f = 0;
    int n_m = 0;
    N_people n_people;
    // Node node;
    Person_name person_name[23];
    // printf("%d\n", n_data);
    freopen("dancer.txt", "r", stdin);
    scanf("%d", &n_data);
    // printf("%d\n", n_data);
    getchar();
    for(i = 0; i < n_data; i++)
    {
        gets(person_name);
        // printf("%s\n", person_name);
        // printf("The gender is %d\n", determineGender(person_name));
        if(determineGender(person_name) == F)
        {
            cutName(person_name);
            n_f++;
            // printf("n_f is %d\n", n_f);
            if(n_f==1)
            {
                // printf("The f_list is %p\n", f_list);
                initial(f_list, person_name);
            }else
            {
                // printf("f_list is %p\n", f_list);
                f_list = insert(f_list, person_name);
                // printf("f_list is %p\n", f_list);
            }
        }else
        {
            cutName(person_name);
            n_m++;
            // printf("n_m is %d\n", n_m);
            if(n_m==1)
            {
                // printf("The m_list is %p\n", m_list);
                initial(m_list, person_name);
            }else
            {
                // printf("m_list is %p\n", m_list);
                // printf("%s\n", person_name);
                m_list = insert(m_list, person_name);
                // printf("m_list is %p\n", m_list);
            }
        }
    }
    n_people.n_f = n_f;
    n_people.n_m = n_m;
    if(n_m > n_f)
    {
        n_people.min_people = n_f;
        n_people.max_people = n_m;
    }
    else
    {
        n_people.min_people = n_m;
        n_people.max_people = n_f;
    }
    return n_people;
}

void printName(Node f_node, Node m_node);
printRound(Linked_list f_list, Linked_list m_list, N_people n_people)
{
    int i, j;
    Node f_node = f_list;
    Node m_node = m_list;
    // printf("f_list->name is %s\n", f_list->name);

    // printf("m_list->name is %s\n", m_list->name);
    // printf("f_node->name is %s\n", f_node->name);
    // printf("m_node->name is %s\n", m_node->name);
    for(i = 0; i < n_round; i++)
    {
        printf("\nRound %d: \n", i+1);
        for(j = 0; j < n_people.min_people; j++)
        {
            printf("%-20s%-20s\n",f_node->name, m_node->name);
            f_node = f_node->next_node;
            m_node = m_node->next_node;
        }
    }
}

int determineGender(Person_name* name)
{
    int name_lenth = strlen(name);
    // printf("name is %s\n", name);
    // printf("The gender is %c\n", name[name_lenth-1]);
    if(name[name_lenth-1]=='F') return F;
    else return M;
}


// void printName(Node f_node, Node m_node)
// {
//     int f_node_name_lenth = strlen(f_node->name)-2;
//     int m_node_name_lenth = strlen(m_node->name)-2;
//     int i;
//     // printf("Start printName\n");
//     // printf("f_node->name is %s\n", f_node->name);
//     // printf("m_node->name is %s\n", m_node->name);
//     // printf("f_node_name_lenth is %d.\n", f_node_name_lenth);
//     // printf("m_node_name_lenth is %d.\n", m_node_name_lenth);
//     m_node->name[m_node_name_lenth] = '\0';
//     f_node->name[f_node_name_lenth] = '\0';
//     printf("%-20s", f_node->name);
//     printf("%-20s\n", m_node->name);
//     // for(i = 0; i < f_node_name_lenth; i++)
//     // {
//     //     printf("%c", f_node->name[i]);
//     // }
//     // printf(" \t\t");
//     // for(i = 0; i < m_node_name_lenth; i++)
//     // {
//     //     printf("%c", m_node->name[i]);
//     // }
//     // printf("\n");
// }

void cutName(Person_name* name)
{
    int name_lenth = strlen(name)-2;
    name[name_lenth] = '\0';
}

int isEmpty(Linked_list list)
{
    // printf("Is empty?\n");
    return list->name[0] == '0';
}

void initial(Linked_list list, Person_name* name)
{
    list->next_node = list;
    strcpy(list->name, name);
    // printf("list->name is %s\n", list->name);
    // printf("The list is %p\n", list);
    // printf("link_list initial success!\n");
}

Linked_list insert(Linked_list list, Person_name* name)
{
    // printf("Start insert\n");
    // printf("name is %s\n", name);
    Node node = (Node)malloc(sizeof(struct Circular_linked_list));
    // printf("The node is %p\n", node);
    strcpy(node->name, name);
    // printf("node->name is %s\n", node->name);
    // printf("The list, list->next_node, node, node->next_node is\t%p\t%p\t%p\t%p\n", list, list->next_node, node, node->next_node);
    node->next_node = list->next_node;
    // printf("The list, list->next_node, node, node->next_node is\t%p\t%p\t%p\t%p\n", list, list->next_node, node, node->next_node);
    list->next_node = node;
    // printf("The list, list->next_node, node, node->next_node is\t%p\t%p\t%p\t%p\n", list, list->next_node, node, node->next_node);
    list = node;
    // printf("The list, list->next_node, node, node->next_node is\t%p\t%p\t%p\t%p\n", list, list->next_node, node, node->next_node);
    // printf("node->name is %s\n", node->name);
    return list;
}
